Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unstable_useFlushEffects hook #34117

Merged
merged 33 commits into from
Feb 18, 2022
Merged

Conversation

devknoll
Copy link
Contributor

@devknoll devknoll commented Feb 9, 2022

Implements #30997 with some minor tweaks to the design:

  • The hook is moved to Client Components (e.g. pages/_app instead of pages/_document). This was a silly oversight in the original design: the hook needs to be called during server prerendering.

  • useFlushEffects instead of useFlushEffect as there isn't a particularly safe way to implement the singular semantics as a Client Component hook given the current implementation of server rendering.


Fixes #30997

@ijjk ijjk added created-by: Chrome Aurora PRs by the Google Chrome team: https://web.dev/aurora type: next labels Feb 9, 2022
@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@devknoll devknoll changed the title Add useFlushEffect Add useFlushEffects hook Feb 14, 2022
@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ijjk
Copy link
Member

ijjk commented Feb 15, 2022

Failing test suites

Commit: c51b271

yarn testheadless test/integration/react-streaming-and-server-components/test/index.test.js

  • Edge runtime - prod > should flush the suffix at the very end
  • Node.js runtime prod > should flush the suffix at the very end
Expand output

● Edge runtime - prod › should flush the suffix at the very end

expect(received).toMatch(expected)

Expected pattern: /<\/body><\/html>/
Received string:  "<!DOCTYPE html><html><head><meta charSet=\"utf-8\"/><meta name=\"viewport\" content=\"width=device-width\"/><meta name=\"next-head-count\" content=\"2\"/><meta name=\"next-font-preconnect\"/><noscript data-n-css=\"\"></noscript></head><body><div id=\"__next\"><!--$?--><template id=\"B:0\"></template><!--/$--></div><div hidden id=\"S:0\"><template id=\"P:1\"></template></div><div hidden id=\"S:1\"><div><h1>component:index.server</h1><div>path:/</div><div>env:env_var_test</div><div>header:</div><div>foo.client<!-- --></div></div></div><script>function $RS(a,b){a=document.getElementById(a);b=document.getElementById(b);for(a.parentNode.removeChild(a);a.firstChild;)b.parentNode.insertBefore(a.firstChild,b);b.parentNode.removeChild(b)};$RS(\"S:1\",\"P:1\")</script><script>function $RC(a,b){a=document.getElementById(a);b=document.getElementById(b);b.parentNode.removeChild(b);if(a){a=a.previousSibling;var f=a.parentNode,c=a.nextSibling,e=0;do{if(c&&8===c.nodeType){var d=c.data;if(\"/$\"===d)if(0===e)break;else e--;else\"$\"!==d&&\"$?\"!==d&&\"$!\"!==d||e++}d=c.nextSibling;f.removeChild(c);c=d}while(c);for(;b.firstChild;)f.insertBefore(b.firstChild,c);a.data=\"$\";a._reactRetry&&a._reactRetry()}};$RC(\"B:0\",\"S:0\")</script><script>(self.__next_s=self.__next_s||[]).push([0,\"/,R:6\"])</script><script>(self.__next_s=self.__next_s||[]).push([1,\"/,R:6\",\"M1:{\\\"id\\\":\\\"./components/foo.client.js\\\",\\\"name\\\":\\\"default\\\",\\\"chunks\\\":[]}\\n\"])</script><script>(self.__next_s=self.__next_s||[]).push([1,\"/,R:6\",\"J0:[\\\"$\\\",\\\"div\\\",null,{\\\"children\\\":[[\\\"$\\\",\\\"h1\\\",null,{\\\"children\\\":\\\"component:index.server\\\"}],[\\\"$\\\",\\\"div\\\",null,{\\\"children\\\":\\\"path:/\\\"}],[\\\"$\\\",\\\"div\\\",null,{\\\"children\\\":\\\"env:env_var_test\\\"}],[\\\"$\\\",\\\"div\\\",null,{\\\"children\\\":\\\"header:\\\"}],[\\\"$\\\",\\\"div\\\",null,{\\\"children\\\":[\\\"$\\\",\\\"@1\\\",null,{}]}]]}]\\n\"])</script>"

   97 |     await fetchViaHTTP(context.appPort, '/').then(async (response) => {
   98 |       const result = await resolveStreamResponse(response)
>  99 |       expect(result).toMatch(/<\/body><\/html>/)
      |                      ^
  100 |     })
  101 |   })
  102 | }

  at integration/react-streaming-and-server-components/test/streaming.js:99:22
  at Object.<anonymous> (integration/react-streaming-and-server-components/test/streaming.js:97:5)

● Node.js runtime prod › should flush the suffix at the very end

expect(received).toMatch(expected)

Expected pattern: /<\/body><\/html>/
Received string:  "<!DOCTYPE html><html><head><meta charSet=\"utf-8\"/><meta name=\"viewport\" content=\"width=device-width\"/><meta name=\"next-head-count\" content=\"2\"/><meta name=\"next-font-preconnect\"/><noscript data-n-css=\"\"></noscript></head><body><div id=\"__next\"><!--$?--><template id=\"B:0\"></template><!--/$--></div><div hidden id=\"S:0\"><template id=\"P:1\"></template></div><div hidden id=\"S:1\"><div><h1>component:index.server</h1><div>path:/</div><div>env:env_var_test</div><div>header:</div><div>foo.client<!-- --></div></div></div><script>function $RS(a,b){a=document.getElementById(a);b=document.getElementById(b);for(a.parentNode.removeChild(a);a.firstChild;)b.parentNode.insertBefore(a.firstChild,b);b.parentNode.removeChild(b)};$RS(\"S:1\",\"P:1\")</script><script>function $RC(a,b){a=document.getElementById(a);b=document.getElementById(b);b.parentNode.removeChild(b);if(a){a=a.previousSibling;var f=a.parentNode,c=a.nextSibling,e=0;do{if(c&&8===c.nodeType){var d=c.data;if(\"/$\"===d)if(0===e)break;else e--;else\"$\"!==d&&\"$?\"!==d&&\"$!\"!==d||e++}d=c.nextSibling;f.removeChild(c);c=d}while(c);for(;b.firstChild;)f.insertBefore(b.firstChild,c);a.data=\"$\";a._reactRetry&&a._reactRetry()}};$RC(\"B:0\",\"S:0\")</script><script>(self.__next_s=self.__next_s||[]).push([0,\"/,R:6\"])</script><script>(self.__next_s=self.__next_s||[]).push([1,\"/,R:6\",\"M1:{\\\"id\\\":\\\"./components/foo.client.js\\\",\\\"name\\\":\\\"default\\\",\\\"chunks\\\":[]}\\n\"])</script><script>(self.__next_s=self.__next_s||[]).push([1,\"/,R:6\",\"J0:[\\\"$\\\",\\\"div\\\",null,{\\\"children\\\":[[\\\"$\\\",\\\"h1\\\",null,{\\\"children\\\":\\\"component:index.server\\\"}],[\\\"$\\\",\\\"div\\\",null,{\\\"children\\\":\\\"path:/\\\"}],[\\\"$\\\",\\\"div\\\",null,{\\\"children\\\":\\\"env:env_var_test\\\"}],[\\\"$\\\",\\\"div\\\",null,{\\\"children\\\":\\\"header:\\\"}],[\\\"$\\\",\\\"div\\\",null,{\\\"children\\\":[\\\"$\\\",\\\"@1\\\",null,{}]}]]}]\\n\"])</script>"

   97 |     await fetchViaHTTP(context.appPort, '/').then(async (response) => {
   98 |       const result = await resolveStreamResponse(response)
>  99 |       expect(result).toMatch(/<\/body><\/html>/)
      |                      ^
  100 |     })
  101 |   })
  102 | }

  at integration/react-streaming-and-server-components/test/streaming.js:99:22
      at runMicrotasks (<anonymous>)
  at Object.<anonymous> (integration/react-streaming-and-server-components/test/streaming.js:97:5)

Read more about building and testing Next.js in contributing.md.

yarn testheadless test/integration/react-18/test/index.test.js

  • Blocking mode > runtime is disabled (dev) > flushes styles as the page renders
  • Blocking mode > runtime is disabled (dev) > flushes custom effects
  • Blocking mode > runtime is disabled (prod) > flushes styles as the page renders
  • Blocking mode > runtime is disabled (prod) > flushes custom effects
Expand output

● Blocking mode › runtime is disabled (dev) › flushes styles as the page renders

TIMED OUT: /red/

undefined

  474 |
  475 |   if (hardError) {
> 476 |     throw new Error('TIMED OUT: ' + regex + '\n\n' + content)
      |           ^
  477 |   }
  478 |   return false
  479 | }

  at Object.check (lib/next-test-utils.js:476:11)
  at integration/react-18/test/common.js:41:7
  at withBrowser (integration/react-18/test/common.js:11:7)
  at Object.<anonymous> (integration/react-18/test/common.js:36:5)

● Blocking mode › runtime is disabled (dev) › flushes custom effects

TIMED OUT: /foo/

undefined

  474 |
  475 |   if (hardError) {
> 476 |     throw new Error('TIMED OUT: ' + regex + '\n\n' + content)
      |           ^
  477 |   }
  478 |   return false
  479 | }

  at Object.check (lib/next-test-utils.js:476:11)
  at integration/react-18/test/common.js:50:7
  at withBrowser (integration/react-18/test/common.js:11:7)
  at Object.<anonymous> (integration/react-18/test/common.js:49:5)

● Blocking mode › runtime is disabled (prod) › flushes styles as the page renders

TIMED OUT: /red/

undefined

  474 |
  475 |   if (hardError) {
> 476 |     throw new Error('TIMED OUT: ' + regex + '\n\n' + content)
      |           ^
  477 |   }
  478 |   return false
  479 | }

  at Object.check (lib/next-test-utils.js:476:11)
  at integration/react-18/test/common.js:41:7
  at withBrowser (integration/react-18/test/common.js:11:7)
  at Object.<anonymous> (integration/react-18/test/common.js:36:5)

● Blocking mode › runtime is disabled (prod) › flushes custom effects

TIMED OUT: /foo/

undefined

  474 |
  475 |   if (hardError) {
> 476 |     throw new Error('TIMED OUT: ' + regex + '\n\n' + content)
      |           ^
  477 |   }
  478 |   return false
  479 | }

  at Object.check (lib/next-test-utils.js:476:11)
  at integration/react-18/test/common.js:50:7
  at withBrowser (integration/react-18/test/common.js:11:7)
  at Object.<anonymous> (integration/react-18/test/common.js:49:5)

Read more about building and testing Next.js in contributing.md.

@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@devknoll devknoll marked this pull request as ready for review February 16, 2022 03:30
@ijjk

This comment has been minimized.

huozhi
huozhi previously approved these changes Feb 17, 2022
@devknoll devknoll changed the title Add useFlushEffects hook Add unstable_useFlushEffects hook Feb 17, 2022
Copy link
Member

@shuding shuding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, just one question.

packages/next/server/render.tsx Outdated Show resolved Hide resolved
@ijjk

This comment has been minimized.

@ijjk

This comment has been minimized.

@ijjk
Copy link
Member

ijjk commented Feb 18, 2022

Stats from current PR

Default Build (Increase detected ⚠️)
General Overall increase ⚠️
vercel/next.js canary azukaru/next.js x-add-use-flush-effect Change
buildDuration 15s 15.2s ⚠️ +152ms
buildDurationCached 6s 6.1s ⚠️ +104ms
nodeModulesSize 359 MB 359 MB ⚠️ +7.36 kB
Page Load Tests Overall increase ✓
vercel/next.js canary azukaru/next.js x-add-use-flush-effect Change
/ failed reqs 0 0
/ total time (seconds) 2.965 2.812 -0.15
/ avg req/sec 843.04 889.06 +46.02
/error-in-render failed reqs 0 0
/error-in-render total time (seconds) 1.15 1.163 ⚠️ +0.01
/error-in-render avg req/sec 2173.71 2149.26 ⚠️ -24.45
Client Bundles (main, webpack, commons)
vercel/next.js canary azukaru/next.js x-add-use-flush-effect Change
450.HASH.js gzip 179 B 179 B
framework-HASH.js gzip 42 kB 42 kB
main-HASH.js gzip 27.9 kB 27.9 kB
webpack-HASH.js gzip 1.44 kB 1.44 kB
Overall change 71.5 kB 71.5 kB
Legacy Client Bundles (polyfills)
vercel/next.js canary azukaru/next.js x-add-use-flush-effect Change
polyfills-HASH.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages
vercel/next.js canary azukaru/next.js x-add-use-flush-effect Change
_app-HASH.js gzip 1.36 kB 1.36 kB
_error-HASH.js gzip 194 B 194 B
amp-HASH.js gzip 312 B 312 B
css-HASH.js gzip 326 B 326 B
dynamic-HASH.js gzip 2.57 kB 2.57 kB
head-HASH.js gzip 350 B 350 B
hooks-HASH.js gzip 919 B 919 B
image-HASH.js gzip 5.05 kB 5.05 kB
index-HASH.js gzip 263 B 263 B
link-HASH.js gzip 2.26 kB 2.26 kB
routerDirect..HASH.js gzip 321 B 321 B
script-HASH.js gzip 383 B 383 B
withRouter-HASH.js gzip 318 B 318 B
85e02e95b279..7e3.css gzip 107 B 107 B
Overall change 14.7 kB 14.7 kB
Client Build Manifests
vercel/next.js canary azukaru/next.js x-add-use-flush-effect Change
_buildManifest.js gzip 460 B 460 B
Overall change 460 B 460 B
Rendered Page Sizes
vercel/next.js canary azukaru/next.js x-add-use-flush-effect Change
index.html gzip 532 B 532 B
link.html gzip 545 B 545 B
withRouter.html gzip 526 B 526 B
Overall change 1.6 kB 1.6 kB

Default Build with SWC (Increase detected ⚠️)
General Overall increase ⚠️
vercel/next.js canary azukaru/next.js x-add-use-flush-effect Change
buildDuration 18.7s 18.6s -86ms
buildDurationCached 6.1s 6s -41ms
nodeModulesSize 359 MB 359 MB ⚠️ +7.36 kB
Page Load Tests Overall increase ✓
vercel/next.js canary azukaru/next.js x-add-use-flush-effect Change
/ failed reqs 0 0
/ total time (seconds) 2.999 2.798 -0.2
/ avg req/sec 833.69 893.52 +59.83
/error-in-render failed reqs 0 0
/error-in-render total time (seconds) 1.163 1.147 -0.02
/error-in-render avg req/sec 2149.05 2179.8 +30.75
Client Bundles (main, webpack, commons)
vercel/next.js canary azukaru/next.js x-add-use-flush-effect Change
450.HASH.js gzip 179 B 179 B
framework-HASH.js gzip 42.1 kB 42.1 kB
main-HASH.js gzip 27.9 kB 27.9 kB
webpack-HASH.js gzip 1.44 kB 1.44 kB
Overall change 71.7 kB 71.7 kB
Legacy Client Bundles (polyfills)
vercel/next.js canary azukaru/next.js x-add-use-flush-effect Change
polyfills-HASH.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages
vercel/next.js canary azukaru/next.js x-add-use-flush-effect Change
_app-HASH.js gzip 1.35 kB 1.35 kB
_error-HASH.js gzip 180 B 180 B
amp-HASH.js gzip 305 B 305 B
css-HASH.js gzip 321 B 321 B
dynamic-HASH.js gzip 2.56 kB 2.56 kB
head-HASH.js gzip 342 B 342 B
hooks-HASH.js gzip 911 B 911 B
image-HASH.js gzip 5.08 kB 5.08 kB
index-HASH.js gzip 256 B 256 B
link-HASH.js gzip 2.28 kB 2.28 kB
routerDirect..HASH.js gzip 314 B 314 B
script-HASH.js gzip 375 B 375 B
withRouter-HASH.js gzip 309 B 309 B
85e02e95b279..7e3.css gzip 107 B 107 B
Overall change 14.7 kB 14.7 kB
Client Build Manifests
vercel/next.js canary azukaru/next.js x-add-use-flush-effect Change
_buildManifest.js gzip 459 B 459 B
Overall change 459 B 459 B
Rendered Page Sizes
vercel/next.js canary azukaru/next.js x-add-use-flush-effect Change
index.html gzip 533 B 533 B
link.html gzip 547 B 547 B
withRouter.html gzip 527 B 527 B
Overall change 1.61 kB 1.61 kB
Commit: 3f38f17

@kodiakhq kodiakhq bot merged commit 944c734 into vercel:canary Feb 18, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
created-by: Chrome Aurora PRs by the Google Chrome team: https://web.dev/aurora type: next
Projects
None yet
Development

Successfully merging this pull request may close these issues.

React 18: Expose useFlushEffect hook for 3rd party CSS-in-JS libraries
4 participants